<?php

/**
 * 合同签约公司仓库
 * @author wangwx 2015-5-14
 */
namespace common\repositories\myhtsq;
use common\repositories\BaseRepository;
use common\entities\myhtsq\ErpBusinessUnitEntity;
use common\entities\myhtsq\InvoiceBusinessUnitEntity;
class ErpBusinessUnitRepository extends BaseRepository{


	/**
	 * 构造方法
	 * @param unknown $config (optional)
	 */
	public function __construct($config = [])
	{
		parent::__construct($config);
	}


	/**
	 * 获取 客户信息-合同签署公司（账套公司）
	 * @param string $keyword 搜索关键字
	 * @param int $limit 返回条数
	 * @return array
	 */
	public static function getCompanies($keyword) {
		$query = ErpBusinessUnitEntity::find()->select("BUGUID,BUName,BUFullName,BUCode")->where(['IsCompany'=>1,'BUType'=>0]);
		if (!empty($keyword)) {
			$query->andWhere(['like', 'BUName', $keyword]);
		}

		return $query->all();
	}

	/**
	 * 根据BUGUID,获取公司名称
	 * @param string $buguid
	 * @return string $buname
	 */
	public static function getBunameByBuguid($buguid) {
		return ErpBusinessUnitEntity::find()->select('BUName')->where(['BUGUID'=>$buguid])->scalar();
	}

    public static function getAllBunameByBuguids($buguids) {
        return ErpBusinessUnitEntity::find()
            ->select('BUName, BUGUID')
            ->where(['BUGUID' => $buguids])
            ->asArray()
            ->all();
    }

    public static function getAreaNameById($buguid) {
        return ErpBusinessUnitEntity::find()
            ->select('BUName, BUGUID')
            ->where(['BUGUID'=>$buguid])
            ->asArray()
            ->groupBy('BUGUID')
            ->all();
    }

	/**
	 * 根据BUGUID,获取区域简称编码
	 * @param string $buguid
	 * @return string $buname
	 */
	public static function getBuCodeByBuguid($buguid) {
		return ErpBusinessUnitEntity::find()->select('BUCode')->where(['BUGUID'=>$buguid])->scalar();
	}

	/**
	 * 根据BUGUID,获取ErpBusinessUnitEntity
	 * @param string $buguid 用户buguid
	 * @return ErpBusinessUnitEntity
	 */
	public static function getBusinessUnitModelByBuguid($buguid) {
		return ErpBusinessUnitEntity::find()->filterWhere(['BUGUID'=>$buguid])->one();
	}

	/**
	 * 根据用户Id获取用户所属公司部门信息
	 * @param string $userguid
	 * @return ErpBusinessUnitEntity
	 */
	public static function getBusinessUnitByUserGuid($userguid) {
		$userInfo = ErpUserRepository::getUserById($userguid);
		$buguid = $userInfo->BUGUID;
		return self::getBusinessUnitModelByBuguid($buguid);
	}

	/**
	 * 根据用户Id获取用户所属公司部门名称
	 * @param string $userguid
	 * @return string
	 */
	public static function getDepartmentNameByBuguid($userguid){
		$userInfo = ErpUserRepository::getUserById($userguid);
		if (empty($userInfo)) {
			return '';
		}
		$buguid = $userInfo->DepartmentGUID;
		$data = self::getBusinessUnitModelByBuguid($buguid);
		if (empty($data->BUName)) {
			return '';
		}else{
			return $data->BUName;
		}
	}

	/**
	 * 返回部门ID
	 * @param $userguid
	 * @return string
	 */
	public static function getDepartmentIdByBuguid($userguid)
	{
		$userInfo = ErpUserRepository::getUserById($userguid);
		if (empty($userInfo)) {
			return '';
		}

		return $userInfo->DepartmentGUID;
	}

	/**
	 * 获取公司ID和子公司ID
	 * @param string $buGuid
	 */
	public static function getSubBuGuidsByBuGuid($buGuid){
		$busUnit = self::getBusinessUnitModelByBuguid($buGuid);
		return self::getSubBuGuids($busUnit->HierarchyCode);
	}

	/**
	 * 获取公司ID和子公司ID
	 * @param string $hierarchyCode
	 */
	public static function getSubBuGuids($hierarchyCode){
		return  ErpBusinessUnitEntity::find()->select('BUGUID')->where(['like', 'HierarchyCode', $hierarchyCode])->distinct()->column();
	}

    /**
     * 通过部门ID，获取部门名称
     * @param $buguid
     * @return array
     */
    public function getDepartmentNameByDepartmentId($buguid)
    {
        return ErpBusinessUnitEntity::find()
            ->select(['BUName', 'BUGUID'])
            ->where([
                'BUGUID' => $buguid,
            ])
            ->all();
    }
}